package com.jrx.anytxn.transaction.job.paph.config;

import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.bean.PaphFileBean;
import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.config.PaphFileConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.AmLoanAccountInfo;
import com.jrx.anytxn.transaction.entity.TlDisbursementLog;
import com.jrx.anytxn.transaction.entity.TlThirdPartyPayment;
import com.jrx.anytxn.transaction.job.paph.step.*;
import com.jrx.anytxn.transaction.service.IPaphFileService;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisPagingItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;

/**
 * 生成同步还款计划分库文件
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
@EnableBatchProcessing
public class GenerateRepayPlanFileConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;
    @Autowired
    private IOrganizationTableService organizationTableService;
    @Autowired
    private GenerateLoanProcessor generateLoanProcessor;
    @Autowired
    private PlanFileWriter planFileWriter;
    @Autowired
    private RepayPlanFileListener repayPlanFileListener;
    @Autowired
    private GenerateOverProcessor generateOverProcessor;
    @Autowired
    private GeneratePayProcessor generatePayProcessor;
    @Autowired
    private IPaphFileService paphFileService;
    @Autowired
    private PaphFileConfig paphFileConfig;

    @Bean
    public Job generateRepayPlanFileJob() {
        return jobBuilderFactory.get("generateRepayPlanFileJob").incrementer(new RunIdIncrementer())
                .listener(repayPlanFileListener)
                .start(generateLoanStep())//同步前一日放款的订单
                .next(generateOverStep())
                .next(generatePayStep())
                .build();
    }

    @Bean
    public Step generateLoanStep(){
        return stepBuilderFactory.get("generateLoanStep")
                .<TlDisbursementLog, PaphFileBean>chunk(anytxnBatchProperties.getGeneratePlanFileConfig().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(generateLoanReader())
                .processor(generateLoanProcessor)
                .writer(planFileWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getGeneratePlanFileConfig().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    public Step generateOverStep(){
        return stepBuilderFactory.get("generateOverStep")
                .<AmLoanAccountInfo, PaphFileBean>chunk(anytxnBatchProperties.getGeneratePlanFileConfig().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(generateOverReader())
                .processor(generateOverProcessor)
                .writer(planFileWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getGeneratePlanFileConfig().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    public Step generatePayStep(){
        return stepBuilderFactory.get("generatePayStep")
                .<TlThirdPartyPayment, PaphFileBean>chunk(anytxnBatchProperties.getGeneratePlanFileConfig().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(generatePayReader())
                .processor(generatePayProcessor)
                .writer(planFileWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getGeneratePlanFileConfig().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public MyBatisPagingItemReader<TlDisbursementLog> generateLoanReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        return new GenerateLoanReader(sqlSessionFactory, org,anytxnBatchProperties.getGeneratePlanFileConfig().getPageSize());
    }

    @Bean
    @StepScope
    public MyBatisPagingItemReader<AmLoanAccountInfo> generateOverReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        return new GenerateOverReader(sqlSessionFactory, org,anytxnBatchProperties.getGeneratePlanFileConfig().getPageSize());
    }

    @Bean
    @StepScope
    public MyBatisPagingItemReader<TlThirdPartyPayment> generatePayReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        return new GeneratePayReader(sqlSessionFactory, org,anytxnBatchProperties.getGeneratePlanFileConfig().getPageSize());
    }

}


